(function () {
    "use strict";
    /**
     * Controllers
     */
    app
        .controller("appCtrl", ["$scope", "$rootScope", "$state", "$translate", "$localStorage", "$window", "$timeout", "RESTService", "AuthService",
            function ($scope, $rootScope, $state, $translate, $localStorage, $window, $timeout, RESTService, AuthService) {
                // add 'ie' classes to html
                var isIE = !!navigator.userAgent.match(/MSIE/i);
                isIE && angular.element($window.document.body).addClass("ie");
                $scope.isSmart = isSmartDevice($window) && angular.element($window.document.body).addClass("smart");
                // config
                $scope.app = {
                    name: "\u8f6c\u8bca\u7cfb\u7edf",
                    version: "1.0.0",
                    // for chart colors
                    color: {
                        primary: "#7266ba",
                        info: "#23b7e5",
                        success: "#27c24c",
                        warning: "#fad733",
                        danger: "#f05050",
                        light: "#e8eff0",
                        dark: "#3a3f51",
                        black: "#1c2b36"
                    },
                    settings: {
                        themeID: 1,
                        navbarHeaderColor: "bg-black",
                        navbarCollapseColor: "bg-white-only",
                        asideColor: "bg-black",
                        headerFixed: true,
                        asideFixed: true,
                        asideFolded: true,
                        asideDock: false,
                        container: false
                    }
                };

                // save settings to local storage
                if (angular.isDefined($localStorage.settings)) {
                    $scope.app.settings = $localStorage.settings;
                }
                else {
                    $localStorage.settings = $scope.app.settings;
                }

                $scope.$watch("app.settings", function () {
                    if ($scope.app.settings.asideDock && $scope.app.settings.asideFixed) {
                        // aside dock and fixed must set the header fixed.
                        $scope.app.settings.headerFixed = true;
                    }

                    // save to local storage
                    $localStorage.settings = $scope.app.settings;
                }, true);

                // angular translate
                $scope.lang = {isopen: false};
                $scope.langs = {zh_CN: "\u4e2d\u6587", en: "English"};
                $rootScope.selectLang = $scope.langs[$translate.proposedLanguage()] || "\u4e2d\u6587";
                $rootScope.isPrdServer = AuthService.isPrdServer();
                RESTService.setLang($rootScope.selectLang);

                // console.log("main --- $scope.selectLang=", $scope.selectLang);
                $scope.setLang = function (langKey, $event) {
                    localStorage.setItem("NG_TRANSLATE_LANG_KEY",langKey);
                    // console.log('ttttttttttttt---', langKey, $event);
                    // // set the current lang
                    // $rootScope.selectLang = $scope.langs[langKey];
                    // // You can change the language during runtime
                    // $translate.use(langKey);
                    // $scope.lang.isopen = !$scope.lang.isopen;
                    // RESTService.setLang($scope.langs[langKey]);
                    window.location.reload();
                };

                $scope.setCurrentUser = function (currUser) {
                    console.log("main --- currentUser=", currUser);
                    $rootScope.currentUser = currUser;
                    $localStorage.currentUser = currUser;
                };

                if (AuthService.isAuthenticated() && angular.isDefined($localStorage.currentUser)) {
                    $rootScope.currentUser = $localStorage.currentUser;
                }

                $scope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) {
                    console.log("main --- toState.name=", toState.name);
                    if ("access.login" === toState.name
                        || "access.forgotpwd" === toState.name
                        || "access.signup" === toState.name
                        || "access.signok" === toState.name
                        || "go" === toState.name
                        || "evaluation" === toState.name
                    ) {
                        return; // go to login page
                    }

                    // user is notlogin
                    var token = RESTService.getAccess_Token();
                    //console.log("main --- token=", token);
                    //console.log("main --- AuthService.isAuthenticated()=", AuthService.isAuthenticated());
                    if (!token) {
                        event.preventDefault(); // cancel default page
                        $state.go("access.login", {from: fromState.name, w: "notLogin"}); // go to login page
                    }
                });


                // $scope.$on('notificationsController_emit_appCtrl', function(event,data) {
                //     $scope.taskSummary_total = data.taskSummary_total;
                //     $scope.notes_count = data.notes_count;
                // });
                // console.log(112345555555555555);
            }]);

})();